Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
Why Django doesn't wrap [] or SELECT to ARRAY in case of using Array field?

For example I have such query as:

Chat.objects.filter(users__contains=[user.pk]).filter(users__contained_by=mentors.values_list('pk', flat=True)) 

This turns to such query:

SELECT "chat_chat"."created_at", "chat_chat"."updated_at", "chat_chat"."id", "chat_chat"."users"
FROM "chat_chat"
 WHERE 
   ("chat_chat"."users" @> [1]::integer[] AND
    "chat_chat"."users" <@ (SELECT V0."id" FROM "users_user" V0
         INNER JOIN "users_userrequest_mentors" V1 ON (V0."id" = V1."user_id")
         WHERE V1."userrequest_id" IN
          (SELECT U0."id" FROM "users_userrequest" U0 WHERE U0."user_id" = 1))::integer[])

If I run this query , I'll face the problem of casting.

cannot cast integer to integer[]

But the documentation of Postgres says that arrays should be used as ARRAY[1,2,...,4]
So I've wrapped [1] and (SELECT ...) to ARRAY like

SELECT "chat_chat"."created_at", "chat_chat"."updated_at", "chat_chat"."id", "chat_chat"."users"
FROM "chat_chat"
 WHERE 
   ("chat_chat"."users" @> ARRAY[1]::integer[] AND
    "chat_chat"."users" <@ ARRAY(SELECT V0."id" FROM "users_user" V0
         INNER JOIN "users_userrequest_mentors" V1 ON (V0."id" = V1."user_id")
         WHERE V1."userrequest_id" IN
          (SELECT U0."id" FROM "users_userrequest" U0 WHERE U0."user_id" = 1))::integer[])

And everything works fine.
I'm just curious why Django (or psycopg2) skip this wrapping. Is there any special meaning for such behavior ?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda